/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.myapp.c0910g.Servlet;

import com.google.gson.Gson;
import com.myapp.c0910g.DAO.PaymentDetailsDAO;
import com.myapp.c0910g.DTO.PaymentDetailsDTO;
import com.myapp.c0910g.entity.PaymentDetails;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author ISTC
 */
public class StatisticServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        try {
            PaymentDetailsDAO payDAO = new PaymentDetailsDAO();
            List<PaymentDetails> listing = new ArrayList<PaymentDetails>();
            List<PaymentDetails> driverList = new ArrayList<PaymentDetails>();
            List<PaymentDetails> advertiseList = new ArrayList<PaymentDetails>();
            listing = payDAO.findAllListing();
            driverList = payDAO.findAllDriver();
            advertiseList = payDAO.findAllAdvertise();
            
            List<PaymentDetailsDTO> listingDTO = new ArrayList<PaymentDetailsDTO>();
            List<PaymentDetailsDTO> driverDTO = new ArrayList<PaymentDetailsDTO>();
            List<PaymentDetailsDTO> advertiseDTO = new ArrayList<PaymentDetailsDTO>();
            for(PaymentDetails p:listing){
                PaymentDetailsDTO payDTO = new PaymentDetailsDTO();
                payDTO.setMoney(p.getPDMoney());
                payDTO.setStatus(p.getPDStatus());
                if(p.getPDStatus() == true){
                    listingDTO.add(payDTO);
                }
            }
            for(PaymentDetails p:driverList){
                PaymentDetailsDTO payDTO = new PaymentDetailsDTO();
                payDTO.setMoney(p.getPDMoney());
                payDTO.setStatus(p.getPDStatus());
                if(p.getPDStatus() == true){
                    driverDTO.add(payDTO);
                }
            }
            for(PaymentDetails p:advertiseList){
                PaymentDetailsDTO payDTO = new PaymentDetailsDTO();
                payDTO.setMoney(p.getPDMoney());
                payDTO.setStatus(p.getPDStatus());
                if(p.getPDStatus() == true){
                    advertiseDTO.add(payDTO);
                }
            }

            List allDTO = new ArrayList();
            allDTO.add(listingDTO);
            allDTO.add(driverDTO);
            allDTO.add(advertiseDTO);

            Gson gson = new Gson();
            String JSON = gson.toJson(allDTO);

            out.print(JSON);
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
